//
//  HomeFMTableViewCell.swift
//  FM
//
//  Created by x on 2017/8/30.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

class HomeFMTableViewCell: UITableViewCell {
    
    /// cell 显示的数据模型
    var model:HomeFM?{
        didSet{
            iconImageView.sd_setImage(with: model?.iconImageURL)
            titlelLable.text = model?.title
            speakLable.text = model?.speak
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //1.添加子控件
        contentView.addSubview(iconImageView)
        contentView.addSubview(titlelLable)
        contentView.addSubview(speakLable)
        
        //2.布局子控件
        var cons:[NSLayoutConstraint] = [NSLayoutConstraint]()
        let tt = ["iconImageView":iconImageView,"titlelLable":titlelLable,"speakLable":speakLable] as [String : Any]
        
        //2.1布局图片
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-5-[iconImageView(44)]", options: .init(rawValue: 0), metrics: nil, views: tt)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-10-[iconImageView(44)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: tt)
        
        //2.2布局titlelLable
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-5-[iconImageView]-[titlelLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: tt)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-10-[titlelLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: tt)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-5-[iconImageView]-[speakLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: tt)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[speakLable]-2-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: tt)
        
        contentView.addConstraints(cons)
    }
    
    /// 计算出cell的高度
    ///
    /// - Parameter model: <#model description#>
    /// - Returns: <#return value description#>
    func rowHeigh(model:HomeFM) -> CGFloat {
        //设置model,重新走一遍model的didSet方法,重新赋值 (看需求)
        self.model = model
        
        //计算高度之后重新布局,要重新布局
        self.layoutIfNeeded()
        
        //返回cell的高度
        return iconImageView.frame.maxY + 2
    }
    //MARK: -- 懒加载
    private lazy var iconImageView:UIImageView = UIImageView.layoutImageView()
        
    private lazy var titlelLable:UILabel = UILabel.lableWith(title: "", titleColor: UIColor.black, fontSize: 14)
    
    private lazy var speakLable:UILabel = UILabel.lableWith(title: "", titleColor: UIColor(white: 0.8, alpha: 1.0), fontSize: 12)
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
